package news

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/dingtalk"

	"github.com/gin-gonic/gin"
)

func InsertNews(c *gin.Context) {

	classId := c.PostForm("class_id")
	title := c.PostForm("title")
	introduce := c.PostForm("introduce")
	content := c.PostForm("content")
	sort := c.PostForm("sort")
	clicks := c.PostForm("clicks")
	isTop := c.PostForm("is_top")

	//获取上传文件
	randomFileName, err := controller.GetUploadFile(c, "fileList")
	if err != nil {
		controller.Fail(c, err.Error())
		return
	}

	var dingtalkNewsStruct dingtalk.DingtalkNewsStruct
	model.CreateModelData(
		model.Db,
		dingtalkNewsStruct.TableName(),
		map[string]interface{}{
			"class_id":   classId,
			"title":      title,
			"introduce":  introduce,
			"content":    content,
			"clicks":     clicks,
			"is_top":     isTop,
			"image_path": randomFileName,
			"sort":       sort,
		},
	)

	controller.Success(c, "")
}
